package web.demo.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import web.demo.entity.Food;
import web.demo.entity.FoodTable;
import web.demo.entity.OrderDetail;
import web.demo.entity.Orders;

public class OrderDetailServlet extends BaseServlet{
	private static List<OrderDetail> orderDetailList;
	static{
		//第一次访问OrderDetailServlet时，创建一个餐车，也就是账单详情集合
		orderDetailList=new ArrayList<OrderDetail>();
	}
	
	protected Object test(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		return null;
	}
	//添加菜品账单详情
	protected Object addOrderDetail(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//计算出总钱数
		double sumMoney=0;
		OrderDetail orderDetail=new OrderDetail();
		int foodId=Integer.parseInt(request.getParameter("foodId"));
		Food food=foodService.getFoodById(foodId);
		orderDetail.setFood_id(foodId);
		//默认食品数量为1
		orderDetail.setFood_count(1);
		orderDetail.setFood_name(food.getName());
		orderDetail.setFood_price(food.getPrice());
		//订单号
		//从session中获取orders
		HttpSession session=request.getSession();
		Orders orders=(Orders)session.getAttribute("orders");
		//从session中获取该clearOrderDetail判断是否应该清空集合
		boolean clearOrderDetail=(boolean) session.getAttribute("clearOrderDetail");
		if(clearOrderDetail){
			orderDetailList.clear();
			clearOrderDetail=false;
			session.setAttribute("clearOrderDetail",clearOrderDetail);
		}
		
		for(OrderDetail o:orderDetailList){
			if(o.getFood_id()==orderDetail.getFood_id()){//判断原集合中是否存在订单详情，若存在，则在原来数量的基础上增加1
				o.setFood_count(o.getFood_count()+1);	//引用传递，不需要重新添加
				orderDetailService.update(o);
			}
			//计算出总钱数
			sumMoney+=o.getFood_countMoney();
		}
		if(!orderDetailList.contains(orderDetail)){//若不存在，则添加到集合中
			orderDetailList.add(orderDetail);
			sumMoney+=orderDetail.getFood_countMoney();
		}
		orders.setTotal_price(sumMoney);
		request.setAttribute("orderDetailList",orderDetailList);
		request.setAttribute("sumMoney",sumMoney);
		
		return request.getRequestDispatcher("app/detail/clientCart.jsp");
	}
	//更改菜品数量
	protected Object modifyFoodCount(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//获取菜品编号和数量，在列表中进行更改
		int foodId=Integer.parseInt(request.getParameter("foodId"));
		int foodCount=Integer.parseInt(request.getParameter("foodCount"));
		double sumMoney=0;
		for(OrderDetail orderDetail:orderDetailList){
			if(orderDetail.getFood_id()==foodId){
				orderDetail.setFood_count(foodCount);
			}
			sumMoney+=orderDetail.getFood_countMoney();
		}
		HttpSession session=request.getSession();
		Orders orders=(Orders) session.getAttribute("orders");
		orders.setTotal_price(sumMoney);
		request.setAttribute("orderDetailList",orderDetailList);
		request.setAttribute("sumMoney",sumMoney);

		return request.getRequestDispatcher("app/detail/clientCart.jsp");
	}
	
	//下单
	protected Object addOrderDetailList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setAttribute("orderDetailList",orderDetailList);
		
		return request.getRequestDispatcher("app/detail/clientOrderList.jsp");
	}
	//结账，清空集合，将账单添加到数据库
	protected Object payBill(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session=request.getSession();
		Orders orders=(Orders) session.getAttribute("orders");
		orders.setStatus(1);
		orders.setOrder_date(new Date());
		//ordersService.update(orders);
		FoodTable foodTable=(FoodTable) session.getAttribute("foodTable");
		foodTable.setStatus(1);
		foodTable.setPrepare_time(new Date());
		foodTableService.update(foodTable);
		boolean result=ordersService.addOrders(orders,orderDetailList);
		if(result==false){
			return "error/error.jsp";
		}else{
			orderDetailList.clear();
			return request.getRequestDispatcher("app/detail/jiezhang.jsp");
		}
	}
}
